package com.example.administrator.wallet.view.fragment.quotation;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.adapter.CurrentDelegateFragmentAdapter;
import com.example.administrator.wallet.base.BaseFragment;
import com.example.administrator.wallet.custom.MessageEvent;
import com.example.administrator.wallet.model.bean.WebSocketPendingOrderBean;
import com.example.administrator.wallet.view.activity.find.cointrans.CoinTransactionDetailsActivity;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * A simple {@link Fragment} subclass.
 */
public class CoinCurrentDelegateFragment extends BaseFragment {


    @Bind(R.id.lv_CurrentDelegateFragment)
    ListView listView;
    private CurrentDelegateFragmentAdapter adapter;
    private List<WebSocketPendingOrderBean.ResultBean.RecordsBean> list;
    private WebSocketPendingOrderBean webSocketPendingOrderBean;
    private Gson mGson;
    private CoinTransactionDetailsActivity activity;
    private int authId;
    private int recordId;
    private String market;


    @Override
    public void init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        activity = (CoinTransactionDetailsActivity) getActivity();
        mGson = new Gson();

        list = new ArrayList<>();
        adapter = new CurrentDelegateFragmentAdapter(getActivity(),list);
        listView.setAdapter(adapter);

        Bundle bundle = getArguments();
        market = bundle.getString("toCurrentDelegationFragment");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(market);
        jsonArray.add(0);
        jsonArray.add(10);
        recordId = activity.sendMeessage("order.query",jsonArray);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent){
        JSONObject jsonObject = (JSONObject) JSONObject.parse(messageEvent.getMessage());
        if (jsonObject.getInteger("id") == recordId){
            webSocketPendingOrderBean = mGson.fromJson(messageEvent.getMessage(), WebSocketPendingOrderBean.class);
            list.clear();
            list.addAll(webSocketPendingOrderBean.getResult().getRecords());

            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public int getLayoutResource() {
        return R.layout.fragment_coin_current_delegate;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

}
